import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token.js'
// import Login from '@/views/Login/index.vue'
// import Layout from '@/views/Layout'
// import Home from '@/views/Home'
// import User from '@/views/User'
// import Search from '@/views/Search/index.vue'
// import SearchResult from '@/views/Search/SearchResults.vue'
// import ArticleDetail from '@/views/ArticleDetail/index.vue'
// import UserEdit from '@/views/User/UserEdit.vue'
// import Chat from '@/views/Chat/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "Login" */ '@/views/Login'),
    beforeEnter (to, from, next) {
      if (getToken()?.length > 0 && to.path === '/login') {
        // vuex里有token(代表登录过, 但是一定要注意过期和主动退出要先清除vuex和本地的token, 让其跳转登录页)
        next('/layout/home')
      } else {
        next()
      }
    }
  },
  {
    path: '/Layout',
    component: () => import(/* webpackChunkName: "Layout" */ '@/views/Layout'),
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName: "home" */ '@/views/Home'),
        meta: {
          scrollT: 0// 保存首页离开时滚动条的位置
        }
      },
      {
        path: 'user',
        component: () => import(/* webpackChunkName: "user" */ '@/views/User')
      }
    ]
  },
  {
    path: '/search',
    component: () => import(/* webpackChunkName: "search" */ '@/views/Search/index.vue')
  },
  {
    path: '/search_result/:kw',
    component: () => import(/* webpackChunkName: "searchResult" */ '@/views/Search/SearchResults.vue')
  },
  {
    // 文章详情
    path: '/detail',
    component: () => import(/* webpackChunkName: "detail" */ '@/views/ArticleDetail/index.vue')
  },
  {
    // 用户信息编辑页面
    path: '/user_edit',
    component: () => import(/* webpackChunkName: "userEdit" */ '@/views/User/UserEdit.vue')
  },
  {
    // 聊天页面
    path: '/chat',
    component: () => import(/* webpackChunkName: "Chat" */ '@/views/Chat/index.vue')
  }
]

const router = new VueRouter({
  routes
})

// 全局导航守卫
// router.beforeEach((to, from, next) => {
//   if (getToken()?.length > 0 && to.path === '/login') {
//     next(false)
//   } else {
//     next()
//   }
// })

export default router
